package com.example.springboottest.example.rest;

import com.alibaba.fastjson.JSONObject;
import com.example.springboottest.student.model.Student;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClientHttpRequestFactory接口主要提供了三种实现方式，
 * 一种是SimpleClientHttpRequestFactory，使用J2SE提供的方式（既java.net包提供的方式）创建底层的Http请求连接，
 * 还有一种方式是使用HttpComponentsClientHttpRequestFactory方式，底层使用HttpClient访问远程的Http服务，使用HttpClient可以配置连接池和证书等信息
 * 还有一种使用OkHttp方式，使用OkHttp3ClientHttpRequestFactory
 */
public class RestTemplateByHttpClientTest {
    public static void main(String[] args) {

//        String AA = "11";
//        System.out.println(AA);
        postStuForEntity();

//        for (int i = 1; i <= 20; i++) {
//            System.out.println("执行第[" + i + "]次");
//            postForEntity();
//            try {
//                System.out.println("slepping...");
//                Thread.sleep(30 * 1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
    }

    public static RestTemplate getRestTemplate() {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setReadTimeout(1 * 5 * 1000);//ms
        factory.setConnectTimeout(1 * 5 * 1000);//ms

        return new RestTemplate(factory);
    }

    public static void postForEntity() {
        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("numbers", "5604051-ZZ01;;;QQQA,5405240-ZZ01;;;QQQA,5407318-ZZ01;;;QQQA,3506211-ZZ01;;;QQQA,6107323-ZZ01;;;QQQA,9101311-ZZ01;;;QQQA,5407321-ZZ01;;;QQQA,6107322-ZZ01;;;QQQA,5405260-ZZ01;;;QQQA,5405200-ZZ01;;;QQQA,9101511-ZZ01;;;QQQA,5407314-ZZ01;;;QQQA,3502320-ZZ01;;;QQQA,5505375-ZZ01;;;QQQA,5401352-ZZ01;;;QQQA,5519015-ZZ02;;;QQQA,2803111-ZZ01;;;QQQA,5604052-ZZ01;;;QQQA,5401480-ZZ01;;;QQQA,5505380-ZZ01;;;QQQA,3506431-ZZ01;;;QQQA,5014101-ZZ01;;;QQQA,6104115-ZZ01;;;QQQA,5409691-ZZ01;;;QQQA,9101111-ZZ01;;;QQQA,5014001-ZZ01;;;QQQA,5401460-ZZ01;;;QQQA,5505420-ZZ01;;;QQQA,5407346-ZZ-A;;;QQQA,5401440-ZZ01;;;QQQA,7901001-ZZ01;;;QQQA,FQ90440-0825;;;QQQA,2803121-ZZ01;;;QQQA,5407663-ZZ01;;;QQQA,5604040-ZZ01(SW);;;QQQA,5521162-ZZ01;;;QQQA,9101001-ZZ01;;;QQQA,3506441-ZZ01;;;QQQA");
        String url = "http://pdmuat.faw.com.cn/Windchill/faw/pdmbom/pdmPartInfos";
        JSONObject result = getRestTemplate().postForObject(url, paramMap, JSONObject.class);
        System.out.println(result.toJSONString());
    }

    public static void getForEntity() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("numbers", "5604051-ZZ01;;;QQQA,5405240-ZZ01;;;QQQA,5407318-ZZ01;;;QQQA");

        String url = "http://pdmuat.faw.com.cn/Windchill/faw/pdmbom/pdmPartInfos?numbers={numbers}";

        ResponseEntity<JSONObject> responseEntity = getRestTemplate().getForEntity(url, JSONObject.class, map);
        JSONObject jsonObject = responseEntity.getBody();

        URI uri = responseEntity.getHeaders().getLocation();
        System.out.println(jsonObject.toString());
    }

    public static void postStuForEntity() {
        Student student = new Student("0", "方宁", 40, "武汉洪山");
//        ResponseEntity<Student> responseEntity = getRestTemplate().postForEntity("http://localhost:9090/stu/create.api"
//                , student, Student.class);

        List list = getRestTemplate().getForObject("http://localhost:9090/stu/findAll.api", List.class);

//         student = getRestTemplate().postForObject("http://localhost:9090/stu/create.api"
//                , student, Student.class);
        System.out.println(list.size());
//        URI uri = responseEntity.getHeaders().getLocation();

//        System.out.println(uri.toString());
//        System.out.println(responseEntity.getBody().toString());
    }
}
